<template>
    <div>
        <h1>食谱新增</h1>
    </div>
    <table class="table">
        <tbody>
            <tr>
                <td>图片上传</td>
                <td>
                    <input type="file" @change="upload">
                    <img :src="axios.defaults.baseURL+data.Img" style="width: 50px;">
                </td>
            </tr>
            <tr>
                <td>菜谱标题</td>
                <td>
                    <input type="text" v-model="data.CookName">
                </td>
            </tr>
            <tr>
                <td>标签</td>
                <td>
                    <select v-model="data.Bq">
                        <option value="0">请选择</option>
                        <option v-for="d in biaolist" :value="d.id">{{d.name}}</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>分类</td>
                <td>
                    <select v-model="data.Tid">
                        <option value="0">请选择</option>
                        <option v-for="d in bangdata" :value="d.Tid">{{d.TypeName}}</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>菜谱详情</td>
                <textarea v-model="data.Xq"></textarea>
            </tr>
            <tr>
                <td></td>
                <td><input type="button" value="新增" @click="add"></td>
            </tr>
        </tbody>
    </table>
</template>

<script setup lang="ts">
import axios from 'axios';
import {ref,onMounted} from 'vue';
import { useRouter } from 'vue-router';
const router=useRouter();
onMounted(() => {
    bang()
})
const upload=(ev:any)=>{
    let f=ev.target.files[0];
    let fd=new FormData();
    fd.append("file",f);


    axios.post("/api/UploadFiles/Upload",fd).then(res=>{
        data.value.Img=res.data;
    })

}
const bangdata=ref({})
const bang=()=>{
    axios.get("/api/Type/GetTypes",bangdata.value).then(res=>{
        bangdata.value=res.data;
    })
}
const biaolist=([{id:1,name:'好菜'},{id:2,name:'烂菜'}])
const data=ref({
    "Id": 0,
  "CookName": "",
  "Img": "string",
  "Bq": 0,
  "Tid": 0,
  "CreateTime": "2025-06-12T11:58:03.567Z",
  "Xq": ""
})
const add=()=>{
    axios.post("/api/Cook/AddCook",data.value).then(res=>{
        if(res.data>0)
        {
            alert('新增成功');
            router.push('/');
        }
        else
        {
            alert('新增失败');
            return;
        }
    })
}





</script>

<style scoped>

</style>